import random

class StringStudy:
    def test_string(self):
        # 产生随机数
        print(random.randrange(0, 100))
        print(random.randrange(0, 10000))
        print(random.randint(0, 1000))

        # 字符串截取
        gg = str("cccdddeeff")
        print(gg[0:3])
        print(gg[-4:-2])
        print(gg[-2:])

        # 字符串大小写转换
        x = str("aabbccdd")
        y = x.upper()
        print(y)
        print(y.lower())
        print(type(y.lower()))

        # 去除前后两端空格，相当与java的trim()
        s = str(" a bbb c ")
        print(s.strip())

        # 打印多行
        ss = """tomorrow is beauty,
               because that is sunny"""
        print(ss)

    def test_capitalize(self):
        # 把首字符转换为大写。
        txt = "hello, and welcome to my world."
        print(txt.capitalize())
        # 把字符串转换为小写。
        print(txt.capitalize().casefold())
        s="AAABBBCCDDDNN"
        print(s.casefold())

    def test_center(self):
        #居中字符串
        txt="banana"
        print(txt.center(20))
        print(txt.center(20,"*"))

    def test_encode(self):
        #字符串编码
        x="sssDDd"
        print(x.encode())
        print(x.encode(encoding="utf-8", errors="ignore"))

    def test_rsplit(self):
        txt = "apple, banana, cherry"
        # 将 max 参数设置为 1，将返回包含 2 个元素的列表！
        x = txt.rsplit(", ", 1)
        s=txt.split(",",1)
        print(x)
        print(s)